from sys import argv
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *


class MyPyOpenGLTest:
    def __init__(self, width=640, height=480, title='画刷'):
        glutInit(len(argv), argv)
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
        glutInitWindowSize(width, height)
        glutInitWindowPosition(350, 200)
        self.window = glutCreateWindow(title.encode('gbk'))
        glutDisplayFunc(self.draw)
        glutReshapeFunc(self.reshape)
        glutMouseFunc(self.mouse_downup)
        glutMotionFunc(self.mouse_move)
        glutKeyboardFunc(self.ascii_key)
        glClearColor(1, 1, 1, 1)

        # 四棱锥的旋转角度

        self.angle4 = 0

        # 立方体的旋转角度
        self.angle6 = 0
        # 四棱锥顶点坐标、绘制测表面时的顶点顺序、每个面上的顶点颜色

        self.points4 = ()
        self.vertices4 = ()
        self.colors4 = ()

        # 立方体顶点坐标、绘制测表面时的顶点顺序、每个面上的顶点颜色
        self.points6 = ()
        self.vertices6 = ()
        self.colors6 = ()

    # 调成图形窗口大小时执行的方法
    def reshape(self, w, h):

        if h== 0:
            h = 1
        ratio = w / h
        glViewport(0, 0, w, h)

        # 切换到透视投影矩阵
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        gluPerspective(45, ratio, 0.1, 10)

        # 切换到模型观察矩阵
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    def draw_pyramind(self):
        # 绘制四棱锥侧面
        glBegin(GL_TRIANGLES)
        for index, vertice in enumerate(self.vertice4):
            glColor3fv(self.colors4[index])
            for i in vertice:
                glVertex3fv(self.points4[i])
            glEnd()

            # 绘制四棱锥底面
            glBegin(GL_QUADS)
            glColor3f(0, 0, 0)
            for vertex in self.points4[1:]:
                glVertex3fv(vertex)
            glEnd()
    def draw_cube(self):
        glBegin(GL_QUADS)



    def draw(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        # 绘制折线图
        glBegin(GL_LINE_STRIP)
        for item in self.data:
            if item is None:
                glEnd()
                glBegin(GL_LINE_STRIP)
            else:
                item_len = len(item)
                if item_len == 1:
                    # 结束上一段折线，修改线宽，开始绘制下一段折线
                    glEnd()
                    glLineWidth(item[0])
                    glBegin(GL_LINE_STRIP)
                elif item_len == 2:
                    glVertex2fv(item)
                elif item_len == 3:
                    # 结束上一段折线，修改颜色，开始绘制下一段折线
                    glEnd()
                    glColor3fv(item)
                    glBegin(GL_LINE_STRIP)
        glEnd()

        glutSwapBuffers()

    # 消息主循环
    def mainloop(self):
        glutMainLoop()


if __name__ == '__main__':
    MyPyOpenGLTest().mainloop()
